package com.jingdianjichi.sunject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.sunject.application.convert.SubjectAnswerDTOConverter;
import com.jingdianjichi.sunject.application.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.sunject.application.dto.SubjectInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目Controller
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        if(log.isInfoEnabled()){
            log.info("add--------SubjectController:{}", JSON.toJSONString(subjectInfoDTO));
        }
        try {
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(),"题目名称不能为空");
            SubjectInfoBo subjectInfoBo =
                    SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            List<SubjectAnswerBo> subjectAnswerBoList =
                    SubjectAnswerDTOConverter.INSTANCE.convertDtoListToBoList(subjectInfoDTO.getOptionList());
            subjectInfoBo.setOptionList(subjectAnswerBoList);
            subjectInfoDomainService.add(subjectInfoBo);
            return Result.ok(true);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController----getSubjectPage:{}",subjectInfoDTO);
            }
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"缺少标签");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"缺少分类");
            SubjectInfoBo subjectInfoBo =
                    SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            subjectInfoBo.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBo.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoBo> boPageResult=subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController----querySubjectInfo:{}",subjectInfoDTO);
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(),"Id不能为空");
            SubjectInfoBo subjectInfoBo =
                    SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            SubjectInfoBo subjectInfoBoNew=subjectInfoDomainService.querySubjectInfo(subjectInfoBo);
            SubjectInfoDTO subjectInfoDtoNew = SubjectInfoDTOConverter.INSTANCE.convertBoToDto(subjectInfoBoNew);
            return Result.ok(subjectInfoDtoNew);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

}
